package com.jame.basics.array;

import org.junit.Test;

import java.util.Arrays;

public class ArrayStudy {
    //创建一个大小为5的int类型数组,方便测试
    int[] arrayI = new int[5];


    /**
     * 获取数组长度
     * 注意:.length不是一个方法
     * 结果:5
     */
    @Test
    public void test1() {
        System.out.println(arrayI.length);
    }

    /**
     * 排序
     * 结果:
     * 1
     * 2
     * 3
     * 4
     * 5
     */
    @Test
    public void test2() {
        arrayI[0] = 2;
        arrayI[1] = 3;
        arrayI[2] = 1;
        arrayI[3] = 5;
        arrayI[4] = 4;
        Arrays.sort(arrayI);
        for (int i : arrayI) {
            System.out.println(i);
        }
    }

    /**
     * copyOf复制数组,返回一个新数组,newlength为新复制的数组大小,如果大于原来数组大小,数值型为0,布尔型为false,字符串为空
     * copyOfRange复制数组,从下标from开始复制，复制到上标to，生成一个新的数组。注意这里包括下标from，不包括上标to
     */
    @Test
    public void test3() {
        arrayI[0] = 1;
        arrayI[1] = 2;
        arrayI[2] = 3;
        arrayI[3] = 4;
        arrayI[4] = 5;
        int[] ints = Arrays.copyOf(arrayI, 6);
        for (int anInt : ints) {
            System.out.println(anInt);
        }
        System.out.println("-------");
        int[] ints1 = Arrays.copyOfRange(arrayI, 2, 4);

        for (int i : ints1) {
            System.out.println(i);
        }
    }


    /**
     * 查询数组中是否包含某个值,找到返回下标,没有找到返回负数,负数减一为下次应该插入的下标
     * fromindex 开始查找下标
     * toindex 查到那
     * 结果:-5
     * 4
     */
    @Test
    public void test4() {
        arrayI[0] = 1;
        arrayI[1] = 2;
        arrayI[2] = 3;
        arrayI[3] = 4;
        arrayI[4] = 5;
        System.out.println(Arrays.binarySearch(arrayI, 1, 4, 5));
        System.out.println(Arrays.binarySearch(arrayI, 5));
    }

    /**
     * 将所有元素设置为v
     * 结果: 全是6
     */
    @Test
    public void test5() {
        arrayI[0] = 1;
        arrayI[1] = 2;
        arrayI[2] = 3;
        arrayI[3] = 4;
        arrayI[4] = 5;
        Arrays.fill(arrayI, 6);
        for (int i : arrayI) {
            System.out.println(i);
        }
    }

    /**
     * 判断两个数组长度相同,并且每个下标对应的值相同,返回true
     * 结果:true
     */
    @Test
    public void test6() {
        arrayI[0] = 1;
        arrayI[1] = 2;
        arrayI[2] = 3;
        arrayI[3] = 4;
        arrayI[4] = 5;
        int[] ints = Arrays.copyOf(arrayI, 5);
        System.out.println(Arrays.equals(arrayI, ints));
    }

    /**
     * 冒泡排序
     * 结果:
     */
    @Test
    public void test7() {
        arrayI[0] = 3;
        arrayI[1] = 4;
        arrayI[2] = 5;
        arrayI[3] = 2;
        arrayI[4] = 1;

        for (int i = arrayI.length; i > 0; i--) {
            for (int j = 0; j < i - 1; j++) {
                int up = arrayI[j];
                int down = arrayI[j + 1];
                if (up > down) {
                    int re = arrayI[j];
                    arrayI[j] = arrayI[j + 1];
                    arrayI[j + 1] = re;
                }

            }
        }
        for (int i : arrayI) {
            System.out.println(i);
        }
    }








    /**
     * 九九乘法表
     *
     * 结果:
     */
    @Test
    public void test8() {
        for(int i=1;i<=9;i++){
            for(int j=1;j<=i;j++){
                System.out.print(j+"*"+i+"="+j*i+"\t");
            }
            System.out.println();
        }
    }

    @Test
    public void test9(){
        int a=1;
        int b=2;
        a+=b;
        b=a-b;
        a=a-b;
        System.out.println(a+"=="+b);
        char[] chars=new char[]{'h','e','l','o'};

        for (int i = 1; i <= chars.length; i++) {
            chars[i-1]= (char) (chars[i-1]+chars[chars.length-i]);
            chars[chars.length-i]=(char)(chars[i-1]-chars[chars.length-i]);
            chars[i-1]=(char)(chars[i-1]-chars[chars.length-i]);
        }
        for (char aChar : chars) {
            System.out.print(aChar);
        }
    }





}
